package cn.hejiang.pay.service.impl;

import cn.hejiang.basic.exception.BusinessException;
import cn.hejiang.basic.service.impl.BaseServiceImpl;
import cn.hejiang.pay.domain.AlipayInfo;
import cn.hejiang.pay.domain.PayBill;
import cn.hejiang.pay.mapper.AlipayInfoMapper;
import cn.hejiang.pay.mapper.PayBillMapper;
import cn.hejiang.pay.service.IPayBillService;
import cn.hejiang.pay.utils.AlipayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PayBillServiceImpl extends BaseServiceImpl<PayBill> implements IPayBillService {

    @Autowired
    private AlipayInfoMapper alipayInfoMapper;

    @Autowired
    private PayBillMapper payBillMapper;

    @Override
    public String pay(PayBill billTmp) {
        // 校验
        if (billTmp == null) {
            throw new BusinessException("支付数据为空！你NT吗？");
        }
        PayBill bill = payBillMapper.loadById(billTmp.getId());
        if (bill == null) {
            throw new BusinessException("支付数据为空！你NT吗？");
        }

        // 区分支付类型   0 余额    1 支付宝    2  微信   3  银联
        switch (bill.getPayChannel().intValue()) {
            case 1: // 支付宝
                AlipayInfo info = alipayInfoMapper.loadByShopId(bill.getShop_id());
                return AlipayUtils.pay(info, bill);
            case 2: // 微信
                // 未上线
                break;
            case 3:
                // 未上线
                break;
            default: // 余额
        }
        return null;
    }

    @Override
    public PayBill loadByUnionPaySn(String unionPaySn) {
        return payBillMapper.loadByUnionPaySn(unionPaySn);
    }
}
